#include "playbacklist.h"
#include <QVBoxLayout>

PlaybackList::PlaybackList(QWidget *parent) : QWidget(parent)
{
    //禁止父窗口影响子窗口样式,必须加上，否则样式不会起作用
    setAttribute(Qt::WA_StyledBackground);

    setFixedWidth(300);
    setVisible(false);
    initPlaylist();


    connect(playbackList, &QListWidget::itemDoubleClicked, this, &PlaybackList::playbackCurrentItem);
}

void PlaybackList::updateCurrentPlayingItem(int idx)
{
    QListWidgetItem *item;
    // 将旧的当前项取消高亮
    if(_oldIdx >= 0){
        item = playbackList->item(_oldIdx);
        item->setBackground(Qt::transparent);
    }

    if(idx >= 0){
        // 将新的当前项高亮
        item = playbackList->item(idx);
        item->setBackground(QColor("#4FACFE")); // 蓝色高亮
        playbackList->setCurrentItem(item);
    }
    _oldIdx = idx;
}

void PlaybackList::showlist()
{
    // todo
    // 将列表项显示出来
    QListWidgetItem *item = new QListWidgetItem("video1");
    //item->setData(Qt::UserRole, filePath); // 存储完整路径
    playbackList->addItem(item);
}

int PlaybackList::getIndex(QListWidgetItem *item)
{
    return playbackList->row(item); // 设置新的当前项
}

void PlaybackList::initPlaylist()
{
    playbackList = new QListWidget(this);

    playbackList->setFixedWidth(300);//限制宽度避免占用过多空间
    playbackList->setStyleSheet(
        "QListWidget { background: #2C3E50; border: none; }"
        "QListWidgetItem { padding: 8px; color: white; }"
        "QListWidgetItem:hover { background: #34495E; }"
    );

    QWidget *titleWidget = new QWidget(this);
    titleWidget->setFixedWidth(300);//限制宽度避免占用过多空间
    titleWidget->setStyleSheet("background-color:rgb(54,54,54)");

    titleTextLabel = new QLabel(titleWidget);
    titleTextLabel->setText("回放列表");
    titleTextLabel->setFixedWidth(120);
    titleTextLabel->setStyleSheet(
            "QLabel{font-family: Microsoft YaHei;"
                "font-size:18px;"
                "color:#BDC8E2;background-color:rgb(54,54,54);}");

    QVBoxLayout *Layout = new QVBoxLayout(this);
    Layout->setContentsMargins(0, 0, 0, 0);
    QHBoxLayout * phLayout = new QHBoxLayout(titleWidget);
    phLayout->setContentsMargins(5,5,5,5);
    phLayout->addWidget(titleTextLabel);
    phLayout->addStretch();//添加一个弹簧

    Layout->addWidget(titleWidget);
    Layout->setSpacing(0); // 设置间距为0
    Layout->addWidget(playbackList);



    connect(playbackList, &QListWidget::itemDoubleClicked, this, &PlaybackList::playbackCurrentItem);
}
